#ifndef OPENGL_WIDGET_H
#define OPENGL_WIDGET_H

#include <QGLWidget>

#include "common/ITexture_image.h"
#include "mvc/model/configuration.h"
#include "camera.h"

class Opengl_widget : public QGLWidget
{
    Q_OBJECT
public:
    explicit Opengl_widget(QWidget *parent = 0);
    ~Opengl_widget();
    void draw_config(const Configuration *config, bool sim, int step);
    int add_texture(ITexture_image *texture);
    inline void enable_uavtexture() {uavtex_enabled = !uavtex_enabled; updateGL();}
    inline void enable_simtexture() {simtex_enabled = !simtex_enabled; updateGL();}
    inline void show_perspective() {camera.change_view(PERSPECTIVE); updateGL();}
    inline void show_ortho() {camera.change_view(ORTHO); updateGL();}

public slots:
    inline void zoom_out() {camera.zoom(DECZ, 1.0); updateGL();}
    inline void zoom_in() {camera.zoom(INCZ, 1.0); updateGL();}
    inline void r_rotate() {camera.rotate_camera(ROTATE_R, 5.0); updateGL();}
    inline void l_rotate() {camera.rotate_camera(ROTATE_L, 5.0); updateGL();}
    inline void u_rotate() {camera.rotate_camera(ROTATE_U, 5.0); updateGL();}
    inline void d_rotate() {camera.rotate_camera(ROTATE_D, 5.0); updateGL();}
    inline void r_move() {camera.move_camera(MOVE_R, 1.0); updateGL();}
    inline void l_move() {camera.move_camera(MOVE_L, 1.0); updateGL();}
    inline void d_move() {camera.move_camera(MOVE_D, 1.0); updateGL();}
    inline void u_move() {camera.move_camera(MOVE_U, 1.0); updateGL();}
    inline void reset_camera() {camera.reset(); updateGL();}

protected:
    void initializeGL();
    void resizeGL(int width, int height);
    void paintGL();
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);

private:
    void enable_texture(int index);

private:
    const Configuration *_config; // the current configuration to draw
    QVector<ITexture_image *> _textures; // the textures asociated to the configuration
    Camera camera; // the camera to view the configuration

    bool uavtex_enabled; // if the configuration texture is enabled
    bool simtex_enabled; // if the simulation texture is enabled
    bool simulate; // if need to draw the simulation
    int cs_step; // the current step of simulation
};

#endif // OPENGL_WIDGET_H
